package com.tangyee.home;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import com.tangyee.R;

public class ReadListView extends ListView implements AbsListView.OnScrollListener{
    //footer
    private View footerView;
    private int footerViewHeight;
    //header
    private View headerView;
    private int headerViewHeight;
    //当前是不是上拉加载中
    public final  int LOAD_MORE=4;
    public final  int LOAD_MORE_REFRESHING = 5;
    private  int currentState2 = LOAD_MORE;
    public ReadListView(Context context) {
        this(context, null);
    }
    public ReadListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public ReadListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initFooterView();
        setOnScrollListener(this);
        loadHeaderView();
    }
    public View getHeaderView(){
        return headerView;
    }
    private void loadHeaderView(){
        headerView = View.inflate(getContext(), R.layout.layout_home_headerview, null);
        headerView.measure(0, 0);
        headerViewHeight = headerView.getMeasuredHeight();
        headerView.setPadding(0,0, 0, 0);
        addHeaderView(headerView);
    }
    private void initFooterView() {
        footerView = View.inflate(getContext(), R.layout.layout_home_footerview, null);
        footerView.measure(0, 0);
        footerViewHeight = footerView.getMeasuredHeight();
        footerView.setPadding(0, -footerViewHeight, 0, 0);
        addFooterView(footerView);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return super.onTouchEvent(ev);
    }

    public interface OnRefreshListener {
        void onRefresh();
    }
    private OnRefreshListener onRefreshListener;
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (scrollState == SCROLL_STATE_FLING || scrollState == SCROLL_STATE_IDLE) {
            if (getLastVisiblePosition() == getCount() - 1  &&  currentState2==LOAD_MORE) {
                currentState2= LOAD_MORE_REFRESHING;
                footerView.setPadding(0, 0, 0, 0);
                setSelection(getCount());
                if (onLoadMoreListener != null) {
                    onLoadMoreListener.onLoadMore();
                }
            }
        }
    }
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    }
    //加载更多回调
    public interface OnLoadMoreListener {
        void onLoadMore();
    }
    private OnLoadMoreListener onLoadMoreListener;
    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
        this.onLoadMoreListener = onLoadMoreListener;
    }
    //结束加载更多
    public void finishLoadMore() {
        footerView.setPadding(0, -footerViewHeight, 0, 0);
        currentState2= LOAD_MORE;
    }

}
